# MakeBy: LiuBoyuan
import json

from basic.player import UserJoin
from framework import Singleton


class _UserManager:
    def __init__(self):
        self.__core: dict = None


class SerableQueen(metaclass=Singleton.Singleton):
    """
    DESCRIPT
        当前回合队列 单例存在
    """

    def __init__(self):
        self.core = {
            "青兰": None,
            "极南": None,
            "歧名": None,
            "龙城": None,
            "太云": None,
            "裂谷塔": None,
            "沙弥": None,
            "西泽": None,
            "斯拉丹": None,
            "坛安": None
        }
        self.lst_core = list(self.core.items())
        self.max_index = len(self.lst_core) - 1
        self.index = 0

    def get_UserManger(self) -> UserJoin:
        """
        获取角色管理器
        :return:
        """
        return UserJoin(self)

    def next_save(self):
        """
        返回下一个角色的信息 并进行保存
        :return:
        """
        self.load()
        ret = self.next_power()
        self.save()
        return ret

    def load(self):
        """
        反序列化对象
        :return:
        """
        try:
            with open("./datas/SerableQueen.json" if __name__ != '__main__' else
                      "../datas/SerableQueen.json", 'r') as f:
                json_ = json.load(f)

            self.core = json_["powerlst"]
            self.index = json_['index']

            self.lst_core = list(self.core.items())
            self.max_index = len(self.lst_core) - 1
        except FileNotFoundError:
            pass  # 没有序列话的时候就不再进行文件的操作
        except json.decoder.JSONDecodeError:
            self.save()

    def save(self):
        """
        序列化对象
        :return:
        """
        json_ = {
            "powerlst": self.core,
            "index": self.index
        }

        with open("./datas/SerableQueen.json" if __name__ != '__main__' else
                  "../datas/SerableQueen.json", 'w') as f:
            json.dump(json_, f)

    def next_power(self):
        """
        返回下一个角色的信息
        :return: 列表（2元素） 第一个表示势力  第二个表示角色信息
        """
        if self.max_index >= self.index:
            self.index += 1
            return self.lst_core[self.index - 1]
        else:
            self.index = 1
            return self.lst_core[0]



if __name__ == '__main__':
    for i in range(200):
        a = SerableQueen()
        a.get_UserManger()
        print(a.next_save())
